<?php
/* -------------------------------------------------------------------------------------
* 	ID:						$Id: xajax.imageprocessing.php 429 2014-01-19 11:43:08Z phone.mueller@googlemail.com $
* 	Letzter Stand:			$Revision: 429 $
* 	zuletzt geaendert von:	Daniel Siekiera
* 	Datum:					29.03.2011
*
* 	SEO:mercari by Siekiera Media
* 	http://www.seo-mercari.de
*
* 	Copyright (c) since 2011 SEO:mercari
* --------------------------------------------------------------------------------------
* 	based on:
* 	(c) 2000-2001 The Exchange Project  (earlier name of osCommerce)
* 	(c) 2002-2003 osCommerce - www.oscommerce.com
* 	(c) 2003     nextcommerce - www.nextcommerce.org
* 	(c) 2005     xt:Commerce - www.xt-commerce.com
*
* 	Released under the GNU General Public License
* ----------------------------------------------------------------------------------- */

require_once(DIR_WS_INCLUDES.'/xajax/xajax_core/xajax.inc.php');
$xajax = new xajax();

$xajax->registerFunction('setProductImages');
$xajax->registerFunction('renderProductImages');
$xajax->registerFunction('setCategoryImages');
$xajax->registerFunction('renderCategoryImages');

include('includes/classes/'.FILENAME_IMAGEMANIPULATOR);

function setProductImages($type, $change_name) {
	$ajax = new xajaxResponse();

	$msg = '';

	if(!is_writeable(DIR_FS_CATALOG . 'images/'))
		$msg = DIR_FS_CATALOG.'<b>images</b>/ ist nicht beschreibbar<br />';

	if(!is_writeable(DIR_FS_CATALOG . 'images/product_images/'))
		$msg .= DIR_FS_CATALOG.'images/<b>product_images</b>/ ist nicht beschreibbar<br />';

	if(!is_writeable(DIR_FS_CATALOG . 'images/product_images/info_images/'))
		$msg .= DIR_FS_CATALOG.'images/product_images/<b>info_images</b>/ ist nicht beschreibbar<br />';

	if(!is_writeable(DIR_FS_CATALOG . 'images/product_images/popup_images/'))
		$msg .= DIR_FS_CATALOG.'images/product_images/<b>popup_images</b>/ ist nicht beschreibbar<br />';

	if(!is_writeable(DIR_FS_CATALOG . 'images/product_images/thumbnail_images/'))
		$msg .= DIR_FS_CATALOG.'images/product_images/<b>thumbnail_images</b>/ ist nicht beschreibbar<br />';
	
	if(!is_writeable(DIR_FS_CATALOG . 'images/product_images/icon_images/'))
		$msg .= DIR_FS_CATALOG.'images/product_images/<b>icon_images</b>/ ist nicht beschreibbar<br />';
	
	if(!empty($msg)) {
		$msg .= '<br />Korrigieren Sie vor dem Neu-Berechnen unbedingt die Schreibrechte dieser/s Ordner/s!';
		$ajax->assign('ajax_product_msg_td', 'innerHTML', $msg);
		$ajax->assign('ajax_product_msg_td', 'style.display', 'block');
		return $ajax;

	} else
		$ajax->assign('ajax_product_msg_td', 'style.display', 'none');

	$files = array();
	if($dir = opendir(DIR_FS_CATALOG_ORIGINAL_IMAGES)){
		while($file = readdir($dir)) {
			if(is_file(DIR_FS_CATALOG_ORIGINAL_IMAGES.$file) && ($file !="index.html") && (strtolower($file) != "thumbs.db") && ($file != '.') && ($file != '..')) {
				$files[] = array('name' =>$file);
				@chmod(DIR_FS_CATALOG_ORIGINAL_IMAGES.$file, 0777);
			}
		}
		closedir($dir);
	}

	ksort($files);

	$gesamtmenge = sizeof($files);

	for($i=0; $i < $gesamtmenge; $i++)
		$ajax->call('xajax_renderProductImages', $gesamtmenge, $i, $files[$i]['name'], $type, $change_name);

	return $ajax;
}

function renderProductImages($total, $i, $name, $type, $change_name) {
	global $db;
	
	@set_time_limit(0);
	
	$ajax = new xajaxResponse();

	$old_name = $name;
	$new_name = '';
	$check = '';

	$getData = $db->db_query("SELECT
									p.products_id,
									pd.products_name
								FROM
									".TABLE_PRODUCTS." p,
									".TABLE_PRODUCTS_DESCRIPTION." pd
								WHERE
									p.products_image = '".$old_name."'
								AND
									pd.products_id = p.products_id
								AND
									pd.language_id = '".$_SESSION['languages_id']."'");

	$getMore = $db->db_query("SELECT
									pi.products_id,
									pi.image_nr,
									pd.products_name
								FROM
									".TABLE_PRODUCTS_DESCRIPTION." pd,
									".TABLE_PRODUCTS_IMAGES." pi
								WHERE
									pd.products_id = pi.products_id
								AND
									pi.image_name = '".$old_name."'
								AND
									pd.language_id = '".$_SESSION['languages_id']."'");

	if(!empty($getData->fields['products_name'])) {
		$checkName = $getData->fields['products_name'];
		$checkID = $getData->fields['products_id'];

	} elseif(!empty($getMore->fields['products_name'])) {
		$checkName = $getMore->fields['products_name'];
		$checkID = $getMore->fields['products_id'];
		$checkNr = $getMore->fields['image_nr'];
	}

	if(empty($getData->fields['products_name']) && empty($getMore->fields['products_name']) && $change_name == 'p_name'){
		@unlink(DIR_FS_CATALOG_ORIGINAL_IMAGES.$old_name);
		@unlink(DIR_FS_CATALOG_THUMBNAIL_IMAGES.$old_name);
		@unlink(DIR_FS_CATALOG_ICON_IMAGES.$old_name);
		@unlink(DIR_FS_CATALOG_INFO_IMAGES.$old_name);
		@unlink(DIR_FS_CATALOG_POPUP_IMAGES.$old_name);
		$ajax->prepend('deleted_image', 'innerHTML', $old_name.'<br />');

	} else {
		
		$pic_name = explode('.', $old_name);
		$suffix = array_pop($pic_name);
		
		if($change_name == 'p_name') {
			require(DIR_FS_INC.'inc.get_url_friendly_text.php');
			
			$new_products_name = get_url_friendly_text($checkName);

			if(!empty($getData->fields['products_name']))
				$new_name = $new_products_name.'.'.strtolower($suffix);

			elseif(!empty($getMore->fields['products_name']))
				$new_name = $new_products_name.'-'.$getMore->fields['image_nr'].'.'.strtolower($suffix);

		} elseif($change_name == 'p_id') {
			if(!empty($getData->fields['products_id']))
				$new_name = $getData->fields['products_id'].'.'.strtolower($suffix);
			elseif(!empty($getMore->fields['products_name']))
				$new_name = $getMore->fields['products_id'].'-'.$getMore->fields['image_nr'].'.'.strtolower($suffix);
		}
		
		@rename(DIR_FS_CATALOG_ORIGINAL_IMAGES.$old_name, DIR_FS_CATALOG_ORIGINAL_IMAGES.$new_name);
		@unlink(DIR_FS_CATALOG_THUMBNAIL_IMAGES.$old_name);
		@unlink(DIR_FS_CATALOG_INFO_IMAGES.$old_name);
		@unlink(DIR_FS_CATALOG_ICON_IMAGES.$old_name);
		@unlink(DIR_FS_CATALOG_POPUP_IMAGES.$old_name);
		
		if(!empty($getData->fields['products_name']))
			$db->db_query("UPDATE ".TABLE_PRODUCTS." SET products_image = '".$new_name."' WHERE products_image = '".$old_name."'");

		elseif(!empty($getMore->fields['products_name']))
			$db->db_query("UPDATE ".TABLE_PRODUCTS_IMAGES." SET image_name = '".$new_name."' WHERE image_name = '".$old_name."'");
			
		$products_image_name = $new_name;
		
		if($type == 'all') {
			require(DIR_WS_INCLUDES.'product_icon_images.php');
			require(DIR_WS_INCLUDES.'product_thumbnail_images.php');
			require(DIR_WS_INCLUDES.'product_info_images.php');
			require(DIR_WS_INCLUDES.'product_popup_images.php');

		} elseif ($type == 'rest') {
			if (!is_file(DIR_FS_CATALOG_ICON_IMAGES.$name))
				require(DIR_WS_INCLUDES.'product_icon_images.php');
			if (!is_file(DIR_FS_CATALOG_THUMBNAIL_IMAGES.$name))
				require(DIR_WS_INCLUDES.'product_thumbnail_images.php');
	        if (!is_file(DIR_FS_CATALOG_INFO_IMAGES.$name))
	        	require(DIR_WS_INCLUDES.'product_info_images.php');
	        if (!is_file(DIR_FS_CATALOG_POPUP_IMAGES.$name))
	        	require(DIR_WS_INCLUDES.'product_popup_images.php');
		}

		$percent = round(((($i+1)*100)/$total), 1);
		$ajax->assign('show_products_process', 'style.width', $percent.'%');
		$ajax->assign('products_precents', 'innerHTML', $percent.'%');
		$ajax->assign('products_img_nr', 'innerHTML', $i+1);
		$ajax->assign('products_img_name_old', 'innerHTML', $old_name);
		$ajax->assign('products_img_name_new', 'innerHTML', $new_name);
	}
	return $ajax;
}

function setCategoryImages() {
	$ajax = new xajaxResponse();

	$files = array();
	if($dir = opendir(DIR_FS_CATALOG_IMAGES.'categories_org/')){
        while($file = readdir($dir))
			if (is_file(DIR_FS_CATALOG_IMAGES.'categories_org/'.$file) && ($file !="index.html") && (strtolower($file) != "thumbs.db") && ($file != '.') && ($file != '..')) {
				$files[]=array('name' => $file);
			}
    	closedir($dir);
    }

	ksort($files);

	$gesamtmenge = sizeof($files);

	for($i=0; $i < $gesamtmenge; $i++)
		$ajax->call('xajax_renderCategoryImages', $gesamtmenge, $i, $files[$i]['name']);

	return $ajax;
}

function renderCategoryImages($total, $i, $name) {
	$ajax = new xajaxResponse();

	$categories_image_name = $name;
	require(DIR_WS_INCLUDES.'category_image.php');

	$percent = round(((($i+1)*100)/$total), 1);
	$ajax->assign('show_category_process', 'style.width', $percent.'%');
	$ajax->assign('category_precents', 'innerHTML', $percent.'%');
	$ajax->assign('category_img_nr', 'innerHTML', $i+1);
	$ajax->assign('category_img_name', 'innerHTML', $name);

	return $ajax;
}

$xajax->processRequest();
?>